/**
 * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug
 * Copyright (c) <spug.dev@gmail.com>
 * Released under the AGPL-3.0 License.
 */
import { observable } from "mobx";
import http from 'libs/http';

class Store {
  @observable records = [];
  @observable permRecords = [];
  @observable messageservices = [];
  @observable messageusers = [];
  @observable record = {};
  @observable idMap = {};
  @observable messageserviceMap = {};
  @observable messageuserMap = {};
  @observable isFetching = false;
  @observable formVisible = false;
  @observable syncVisible = false;
  @observable showTemplate = false;

  @observable messageservice_id;
  @observable messageuser_id;
  @observable f_name;
  @observable f_messageservice_id;

  switchTemplate = () => {
    this.showTemplate = !this.showTemplate
  };

  fetchRecords = () => {
    this.isFetching = true;
    return http.get('/api/message/push/')
      .then(({message_push, perms, message_service, message_user}) => {
        this.records = message_push;
        this.messageservices = message_service
        this.messageusers = message_user
        this.permRecords = message_push.filter(item => perms.includes(item.id));
        for (let item of message_push) {
          this.idMap[item.id] = item
        }
        for (let item of message_service) {
          this.messageserviceMap[item.id] = item.name
        }
        for (let item of message_user) {
          this.messageuserMap[item.userid] = item.name
        }
      })
      .finally(() => this.isFetching = false)
  };

  showForm = (info = {}) => {
    this.formVisible = true;
    this.record = info
  }
}

export default new Store()
